import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import dynamicProxy from './vite.dynamic.proxy'
import legacy from '@vitejs/plugin-legacy'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import pxtorem from 'postcss-pxtorem'

const timestamp = new Date().getTime()

export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    dynamicProxy({
      path: new RegExp('^/seer'),
      default: 'https://mock.apifox.cn/m1/3332315-0-default/'
    }),
    legacy({
      targets: ['ie >= 11'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime']
    }),
    // 按需加载Vant组件
    Components({
      resolvers: [VantResolver()]
    })
  ],
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/style/var.scss" as *;`,
        sassOptions: {
          outputStyle: 'expanded'
        }
      }
    },
    postcss: {
      plugins: [
        pxtorem({
          rootValue: 37.5, // 基于375设计稿（1rem=37.5px）
          propList: ['*'],
          exclude: (file) =>
            // 只会转换 带mobile文件name的文件 
            !file.includes('mobile') || file.includes('node_modules') || file.includes('vant')
        })
      ]
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  base: '/',
  server: {
    host: '0.0.0.0',
    port: 9988
  },
  build: {
    target: ['es2015', 'edge88', 'firefox78', 'chrome87', 'safari13'],
    rollupOptions: {
      output: {
        entryFileNames: `assets/[name].${timestamp}.js`,
        chunkFileNames: `assets/[name].${timestamp}.js`,
        assetFileNames: `assets/[name].${timestamp}.[ext]`
      }
    }
  }
})
